package com.szqhyxj.youxiangju.biz.plan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.core.SharedPreferencesUtil;
import com.szqhyxj.youxiangju.biz.core.UserInfoManger;
import com.szqhyxj.youxiangju.biz.equipmentActivation.equipmentCardslot.EquipmentCardSlotActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.MerchantContainerActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.deviceinfo.DeviceInfo;
import com.szqhyxj.youxiangju.biz.plan.fragment.BilledFragment;
import com.szqhyxj.youxiangju.biz.plan.fragment.UnbilledFragment;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by leung on 2018/11/4.
 */

public class RepaymentActivity extends BaseActivity implements RepaymentContract.View, NavigationBar.NavigationBarListener {

    @BindView(R.id.billarea)
    RadioGroup bill;
    @BindView(R.id.nettitle)
    TextView nettitle;
    @BindView(R.id.netArea)
    LinearLayout netArea;
    @BindView(R.id.neticon)
    ImageView neticon;
    @BindView(R.id.topbg)
    View topbg;
    @BindView(R.id.paln_line)
    View palnLine;
    @BindView(R.id.guider01)
    LinearLayout guider01;
    @BindView(R.id.guider02)
    LinearLayout guider02;
    @BindView(R.id.guider03)
    LinearLayout guider03;

    public static void start(Activity activity) {
        Intent intent = new Intent();
        intent.setClass(activity, RepaymentActivity.class);
        activity.startActivity(intent);

    }

    @BindView(R.id.repayarea)
    FrameLayout repayarea;
    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.billed)
    RadioButton billed;
    @BindView(R.id.unbilled)
    RadioButton unbilled;
    private BilledFragment billedFragment;
    private UnbilledFragment unbilledFragment;

    RepaymentPresenter presenter;
    ArrayList<RepaymentModel.RepaymentEntity> listbilled = new ArrayList<>();
    ArrayList<RepaymentModel.RepaymentEntity> listUnbilled = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repayment);
        ButterKnife.bind(this);
        presenter = new RepaymentPresenter(this);
        presenter.init();
        initEvent();
        navigationBar.setNavigationBarListener(this);
        FragmentManager fragmentManager = getSupportFragmentManager();
        final FragmentTransaction transaction = fragmentManager.beginTransaction();
        billedFragment = BilledFragment.newInstance(listbilled);
        unbilledFragment = UnbilledFragment.newInstance(listUnbilled);
        // 先添加再隐藏起来
        transaction.add(R.id.repayarea, billedFragment);
        transaction.hide(billedFragment);
        transaction.add(R.id.repayarea, unbilledFragment);
        transaction.hide(unbilledFragment);
        // 默认显示第一个
        transaction.show(billedFragment);
        // 提交事务
        transaction.commit();
        presenter.setdeviceinfo();
        if (SharedPreferencesUtil.getRepaymentShow()) {
            navigationBar.setVisibility(View.VISIBLE);
            topbg.setVisibility(View.VISIBLE);
            netArea.setVisibility(View.VISIBLE);
            bill.setVisibility(View.VISIBLE);
            palnLine.setVisibility(View.VISIBLE);
            repayarea.setVisibility(View.VISIBLE);
            guider01.setVisibility(View.GONE);
            guider02.setVisibility(View.GONE);
            guider03.setVisibility(View.GONE);
        } else {
            navigationBar.setVisibility(View.GONE);
            topbg.setVisibility(View.GONE);
            netArea.setVisibility(View.GONE);
            bill.setVisibility(View.GONE);
            palnLine.setVisibility(View.GONE);
            repayarea.setVisibility(View.GONE);
            guider01.setVisibility(View.VISIBLE);
            guider02.setVisibility(View.GONE);
            guider03.setVisibility(View.GONE);

        }


    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.init();
    }


    @OnClick({R.id.billed, R.id.unbilled, R.id.netArea, R.id.next01, R.id.next02, R.id.know01})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.billed:
                presenter.BilledCardFragment();
                break;
            case R.id.unbilled:
                presenter.UnbilledCardFragment();
                break;
            case R.id.netArea:
                EquipmentCardSlotActivity.start(this);
                break;
            case R.id.next01:
                guider01.setVisibility(View.GONE);
                guider02.setVisibility(View.VISIBLE);
                guider03.setVisibility(View.GONE);
                break;
            case R.id.next02:
                guider01.setVisibility(View.GONE);
                guider02.setVisibility(View.GONE);
                guider03.setVisibility(View.VISIBLE);
                break;
            case R.id.know01:
                guider03.setVisibility(View.GONE);
                navigationBar.setVisibility(View.VISIBLE);
                topbg.setVisibility(View.VISIBLE);
                netArea.setVisibility(View.VISIBLE);
                bill.setVisibility(View.VISIBLE);
                palnLine.setVisibility(View.VISIBLE);
                repayarea.setVisibility(View.VISIBLE);
                SharedPreferencesUtil.setRepaymentShow(true);
                break;
        }
    }

    private void initEvent() {
        bill.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {

            }
        });


    }

    @Override
    public void showList(List<RepaymentModel.RepaymentEntity> list) {
        listbilled.clear();
        listUnbilled.clear();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getBillStates() == 1) {
                listbilled.add(list.get(i));
            }
            if (list.get(i).getBillStates() == 0) {
                listUnbilled.add(list.get(i));
            }
        }

        billedFragment.update();
        unbilledFragment.update();
        Log.w("======", listbilled.size() + "     " + listUnbilled.size());
        billed.setText("已出账单 (" + listbilled.size() + ")");
        unbilled.setText("未出账单 (" + listUnbilled.size() + ")");
    }

    @Override
    public void showBilledCardFragment() {
        getSupportFragmentManager().beginTransaction().hide(unbilledFragment).show(billedFragment).commit();

    }

    @Override
    public void showUnbulledCardFragment() {
        getSupportFragmentManager().beginTransaction().hide(billedFragment).show(unbilledFragment).commit();

    }

    @Override
    public void showToast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showuserinfo() {
        if (UserInfoManger.INSTANCE.getUser().getActiviteStatus() == 0) {
            netArea.setVisibility(View.GONE);
        } else {
            netArea.setVisibility(View.VISIBLE);
            if (DeviceInfo.Holder.getInstance().getOnlineState().equals("1")) {
                neticon.setImageDrawable(getResources().getDrawable(R.mipmap.poswangluo));
                nettitle.setText(DeviceInfo.Holder.getInstance().getAepnType());
                nettitle.setTextColor(getResources().getColor(R.color.white));
            } else {
                neticon.setImageDrawable(getResources().getDrawable(R.mipmap.poswangluo1));
                nettitle.setText("离线");
                nettitle.setTextColor(getResources().getColor(R.color.texttm));
            }


        }
    }

    @Override
    public void enshowuserInfo() {
        netArea.setVisibility(View.GONE);
    }

    @Override
    public void navigationLeft() {
        MerchantContainerActivity.start(this, true);
    }

    @Override
    public void navigationRight() {

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            MerchantContainerActivity.start(this, true);
        }
        return super.onKeyDown(keyCode, event);
    }

}
